<?php

namespace sixteen::dom;

class element
{
	protected $attributes = array();
	protected $children;
	protected $events;
	protected $innerHTML;
	protected $parent;
	protected $tag;
	protected $isSelfClosing = false;
	
	function __construct()
	{
		self::initAttributes();
	}

	function __destruct(){}
	
	function initAttributes()
	{
		$this->attributes = array_merge(
			$this->attributes,
			array(
				'id' => null,
				'class' => array(),
				'lang' => null,
				'style' => null
			)
		);
	}
	
	function addClass($className)
	{
		if (in_array($className, $this->attributes['class']))
		{
			return false; // allready exists
		}
		else
		{
			$this->attributes['class'][] = $className;
			return true; // successfully added
		}
	}
	
	function removeClass($className)
	{
		if (in_array($className, $this->attributes['class']))
		{
			unset($this->attributes['class'][$className]);
			return true; // successfully removed
		}
		else
		{
			return false; // class doesn't exist
		}	
	}
	
	function getClass()
	{
		return $this->attributes['class']; // return all classes as array
	}


	function setAttribute($key, $value)
	{
		if(array_key_exists($key, $this->attributes))
		{
			$this->attributes[$key] = $value;
			return true; // attribute can be set on the element and has been set
		}
		else
		{
			return false; // attribute cannot be set on the element
		}
	}
	
	function removeAttribute($key)
	{
		if(array_key_exists($key, $this->attributes))
		{
			if($this->attributes[$key])
			{
				$this->attributes[$key] = null;
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	function getAttribute($key)
	{
		return $this->attributes[$key];
	}
	
	
	function render()
	{
		$markupPairs = array();
		foreach ($this->attributes as $key=>$value)
		{
			if ($value)
			{
				if (is_array($value))
				{
					$value = implode(' ', $value);
				}
				$markupPairs[] = $key . '="' . $value . '"';
			}
		}
		if ($this->isSelfClosing)
		{
			$markup = '<' . $this->tag . ' ' . implode(' ', $markupPairs) .'/>';
		}
		else
		{
			$markup = '<' . $this->tag . ' ' . implode(' ', $markupPairs) . '>' . $this->renderInnerHtml() . '</' . $this->tag . '>';
		}
		return $markup;
	}
	
	function validateMarkup(){}
	
	function addChild($child)
	{
		if(is_object($child))
		{
			if($child->tag)
			{
				$this->children[] = $child;
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	function removeChild()
	{
		
	}
	
	function renderInnerHtml()
	{
		if (count($this->children)>0)
		{
			foreach($this->children as $child)
			{
				$childrenMarkup[] = $child->render();
			}
			return "\n" . implode("\n", $childrenMarkup) . "\n";
		}
		else if ($this->innerHTML)
		{
			return $this->innerHTML;
		}
		else
		{
			return null;
		}
	}
	
	function setInnerHTML($innerHTML=null)
	{
		$this->innerHTML = $innerHTML;
		return true;
	}

	function getInnerHTML()
	{
		return $this->innerHTML;
	}
	
}
?>
